package com.hgyc.mom.tool.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hgyc.mom.common.context.FileServiceContext;
import com.hgyc.mom.common.exception.BizException;
import com.hgyc.mom.tool.entity.SopAppVersion;
import com.hgyc.mom.tool.mapper.SopAppVersionMapper;
import com.hgyc.mom.tool.service.SopAppVersionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Objects;

/**
 * sop版本管理Service业务层实现
 *
 * @author fwj
 * @since 2025-07-28
 */
@Slf4j
@Service
public class SopAppVersionServiceImpl extends ServiceImpl<SopAppVersionMapper, SopAppVersion> implements SopAppVersionService {

    @Autowired
    private FileServiceContext fileServiceContext;

    @Override
    public void saveVersion(SopAppVersion sopAppVersion) {
        SopAppVersion oldVersion = getByVersion(sopAppVersion.getVersion());
        if (!Objects.isNull(oldVersion)) {
            throw new BizException("版本号已存在！");
        }
        save(sopAppVersion);
    }

    @Override
    public void updateVersion(SopAppVersion sopAppVersion) {
        updateById(sopAppVersion);
    }

    @Override
    public void removeVersion(List<Long> ids) {
        // TODO 其他逻辑
        removeByIds(ids);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String importData(List<SopAppVersion> sopAppVersionList, boolean updateSupport) {
        if (sopAppVersionList == null || sopAppVersionList.isEmpty()) {
            throw new IllegalArgumentException("导入数据不能为空");
        }

        try {
            if (updateSupport) {
                // TODO 实现更新已有数据的导入逻辑
                return "更新导入成功，共导入 " + sopAppVersionList.size() + " 条数据";
            } else {
                // TODO 实现不更新已有数据的导入逻辑
                return "新增导入成功，共导入 " + sopAppVersionList.size() + " 条数据";
            }
        } catch (Exception e) {
            log.error("sop版本管理导入失败", e);
            throw new BizException("sop版本管理导入失败：" + e.getMessage());
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteApp(List<Long> list) {
        for (Long id : list) {
            SopAppVersion app = getById(id);
            if (app != null && app.getFilePath() != null) {
                fileServiceContext.deleteFile(app.getFilePath());
            }
        }
        removeByIds(list);
    }

    @Override
    public String lastVersion() {
        LambdaQueryWrapper<SopAppVersion> qw = new LambdaQueryWrapper<>();
        qw.orderByDesc(SopAppVersion::getCreateBy);
        qw.last("limit 1");
        SopAppVersion appVersion = getOne(qw, false);

        if (appVersion != null && appVersion.getVersion() != null) {
            return appVersion.getVersion();
        }
        return null;
    }

    @Override
    public SopAppVersion getByVersion(String version) {
        LambdaQueryWrapper<SopAppVersion> qw = new LambdaQueryWrapper<>();
        qw.eq(SopAppVersion::getVersion, version);
        SopAppVersion appVersion = getOne(qw, false);
        return appVersion;
    }
}
